// pages/search/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({
  data: {
   searchResults:[],
   isShow:false,
   value:""
  },

  timeId: -1,
  handleInput(e){
    // console.log(e);
    const {value} = e.detail;
    if(!value.trim()){
      // 为空，直接return
      this.setData({
        isShow: false,
        searchResults:[]
      });
      return;
    }else{
      // 不为空，发送查询请求
      clearTimeout(this.timeId);
      this.timeId = setTimeout(() => {
        this.getSearchResult(value);
      }, 1000);
      this.setData({
        isShow: true,
        value
      })
    }
  },

  async getSearchResult(query){
    let searchResults = await request({url:"/goods/qsearch", data:{query}});
    this.setData({
      searchResults
    })
  },

  handleCancel(){
    this.setData({
      searchResults:[],
      isShow:false,
      value:""
    })
  }
})